package com.edclol.apitest.tableapi;

import com.edclol.apitest.tableapi.pojo.Active_day;
import com.edclol.apitest.tableapi.pojo.Data;
import com.edclol.apitest.tableapi.pojo.JsonRootBean;
import com.google.gson.Gson;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer011;

import java.util.List;
import java.util.Properties;

public class TableTest4_KafkaPipeLine3 {
    public static void main(String[] args) throws Exception {
//        writeToKafka("json1","/json.txt");
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "node105:9092");
        properties.setProperty("group.id", "consumer-group5");
        properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
//        properties.setProperty("auto.offset.reset", "latest");
        properties.setProperty("auto.offset.reset", "earliest");
//        JsonRowDeserializationSchema
        // 读取数据
        DataStream<String> inputStream = env.addSource(new FlinkKafkaConsumer011<String>("json1", new SimpleStringSchema(), properties));


        SingleOutputStreamOperator<JsonRootBean> dataStream = inputStream.map(line -> {
            Gson gson = new Gson();
            JsonRootBean bean = gson.fromJson(line, JsonRootBean.class);
            Data data = bean.getData();

            List<Active_day> day = data.getActive_day();
            System.out.println(gson.toJson(day));
            return bean;
        });

        dataStream.forceNonParallel();

//        dataStream.print();

        env.execute();
    }
}
